async function sendWechatMsg(obj) {
	const {
		Network,
		Page,
		DOM,
		Runtime,
		Emulation
	} = this.client;
	var {
		result
	} = await Runtime.evaluate({
		expression: 'location.href'
	});
	if (result.value != 'https://wx2.qq.com/') {
		return new Promise((resolve, reject) => {
			this.loginWechat().then(result => {
				this.sendWechatMsg().then(result => {
					resolve(result)
				}, result => {
					reject(result)
				})
			}, result => {
				reject(result)
			})
		})
	}
	await Runtime.evaluate({
		expression: `function fireKeyEvent(el, evtType, keyCode){
    var doc = el.ownerDocument,  
        win = doc.defaultView || doc.parentWindow,  
        evtObj;  
    if(doc.createEvent){  
        if(win.KeyEvent) {  
            evtObj = doc.createEvent('KeyEvents');  
            evtObj.initKeyEvent( evtType, true, true, win, false, false, false, false, keyCode, 0 );  
        }  
        else {  
            evtObj = doc.createEvent('UIEvents');  
            Object.defineProperty(evtObj, 'keyCode', {  
                get : function() { return this.keyCodeVal; }  
            });       
            Object.defineProperty(evtObj, 'which', {  
                get : function() { return this.keyCodeVal; }  
            });  
            evtObj.initUIEvent( evtType, true, true, win, 1 );  
            evtObj.keyCodeVal = keyCode;  
            if (evtObj.keyCode !== keyCode) {  
                console.log("keyCode " + evtObj.keyCode + " 和 (" + evtObj.which + ") 不匹配");  
            }  
        }  
        el.dispatchEvent(evtObj);  
    }   
    else if(doc.createEventObject){  
        evtObj = doc.createEventObject();  
        evtObj.keyCode = keyCode;  
        el.fireEvent('on' + evtType, evtObj);  
    }  
} `
	});
	await Runtime.evaluate({
		expression: `document.querySelector('#search_bar input').value = '` + obj.who + `'`
	});
	await Runtime.evaluate({
		expression: `fireKeyEvent(document.querySelector('#search_bar input'), 'input');`
	});
	return step2.call(this, obj)
}

async function step2(obj) {
	const {
		Network,
		Page,
		DOM,
		Runtime,
		Emulation
	} = this.client;
	var {
		result
	} = await Runtime.evaluate({
			expression: `document.querySelectorAll('#search_bar>div').length`
		});
	if (result.value != 1) {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				step2.call(this, obj).then(result => {
					resolve(result)
				}, result => {
					reject(result)
				})
			}, 1000)
		})
	}
	await Runtime.evaluate({
		expression: `fireKeyEvent(document.querySelector('#search_bar input'), 'keydown', 13);`
	});
	await Runtime.evaluate({
		expression: `document.querySelector('#editArea').innerHTML = '` + obj.what + `'`
	});
	await Runtime.evaluate({
		expression: `fireKeyEvent(document.querySelector('#editArea'), 'input');`
	});
	await Runtime.evaluate({
		expression: `fireKeyEvent(document.querySelector('#editArea'), 'keydown', 13);`
	});
	return new Promise(resolve => {
		resolve()
	})
}

module.exports = sendWechatMsg
